package arkham.main;

import java.awt.Insets;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.xml.bind.*;
import java.util.*;
import javax.swing.*;

import arkham.data.*;
import arkham.data.Board.Location;
import arkham.data.Board.OtherWorld;
import arkham.jaxb.ancientone.AOSetType;
import arkham.jaxb.board.*;
import arkham.jaxb.encounter.*;
import arkham.jaxb.monster.MonsterSetType;
import arkham.net.ServerBase;
import arkham.util.ImageUtils;
import arkham.gui.GameOptionsDialog;
import arkham.gui.LobbyScreen;
import arkham.gui.ViewManager;
import arkham.gui.WelcomeScreen;

public class Test {
	
	public static void main(String[] args) throws Exception
	{
		WelcomeScreen wscreen = new WelcomeScreen();
		/*
		GameData gd = loadGameData(6);
		loadSampleStuff(gd);
		final ViewManager vm = new ViewManager(gd);
		final JFrame jf = new JFrame("Arkham");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.add(vm.getMainPanel());
		jf.setSize(816, 665);
		jf.setIconImage(ImageUtils.loadImage("img/ahicon.png"));
		jf.addMouseMotionListener(new MouseMotionListener()
		{
			public void mouseMoved(MouseEvent e)
			{
				JPanel jp = vm.getMapPanel();
				Point mp = jp.getMousePosition();
				if (mp != null)
				{
					vm.getBoardManager().mouseAt(mp.x, mp.y);
					vm.getMouseOverPanel().repaint();
				}
			}
			
			public void mouseDragged(MouseEvent e)
			{
				
			}
		});
		jf.setVisible(true);
		*/
		
	}
	
	public static void loadSampleStuff(GameData gdata)
	{
		Board b = gdata.getMainBoard();
		Location loc = b.getLocation("Woods");
		loc.setClues(2);
		loc = b.getLocation("Independence Square");
		gdata.openNewGateOn(loc);
		loc = b.getLocation("Hibb's Roadhouse");
		loc.setClosed(true);
		loc = b.getLocation("Unvisited Isle");
		loc.setSealed(true);
		Investigator inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Sister Mary");
		loc = b.getLocation("South Church");
		gdata.moveInvestigatorTo(inv, loc);
		loc = b.getLocation("Uptown");
		Monster mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, loc);
		mon = GameData.removeByName(gdata.getMonsters(), "Ghost");
		gdata.moveMonsterTo(mon, loc);
		loc = b.getLocation("Graveyard");
		
		mon = GameData.removeByName(gdata.getMonsters(), "Mi-go");
		gdata.moveMonsterTo(mon, loc);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, loc);
		gdata.openNewGateOn(loc);
		loc = b.getLocation("Black Cave");
		loc.setSealed(true);
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Jenny Barnes");
		gdata.moveInvestigatorTo(inv, loc);
		
		OtherWorld ow = gdata.getMainBoard().getOtherWorld("Plateau of Leng");
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Joe Diamond");
		gdata.moveInvestigatorTo(inv, ow.getSegment(1));
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Vincent Lee");
		gdata.moveInvestigatorTo(inv, ow.getSegment(1));
		
		loc = b.getLocation("Administration");
		gdata.openNewGateOn(loc);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, loc);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, loc);
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Mandy Thompson");
		gdata.moveInvestigatorTo(inv, loc);
		LocationContainer con = b.getLocationContainer(Board.SKY_NAME);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, con);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, con);
		con = b.getLocationContainer("Science Building");
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Bob Jenkins");
		gdata.moveInvestigatorTo(inv, con);
		inv = GameData.retrieveByName(gdata.getInvestigatorPool(), "Monterey Jack");
		gdata.moveInvestigatorTo(inv, con);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, con);
		mon = gdata.getMonsters().remove(0);
		gdata.moveMonsterTo(mon, con);
		
	}
	
	/*
	public static GameData loadGameData(int numPlayers)
	{
		GameData gdata = new GameData(numPlayers);
		ArrayList<String> filenames1 = new ArrayList<String>();
		filenames1.add("xml/base/basecards.xml");
		gdata.loadInvCardsFromFiles(filenames1);
		ArrayList<String> filenames2 = new ArrayList<String>();
		filenames2.add("xml/base/arkhamchars.xml");
		gdata.loadInvestigators(filenames2);
		ArrayList<String> filenames3 = new ArrayList<String>();
		filenames3.add("xml/base/arkhammonsters.xml");
		gdata.loadMonsters(filenames3);
		ArrayList<String> filenames4 = new ArrayList<String>();
		filenames4.add("xml/base/arkhamaos.xml");
		gdata.loadAncientOnes(filenames4);
		ArrayList<String> filenames5 = new ArrayList<String>();
		gdata.loadBoards("xml/base/arkhamboard.xml", filenames5);
		filenames5.add("xml/base/resources.xml");
		gdata.loadResources(filenames5);
		ArrayList<String> filenames6 = new ArrayList<String>();
		filenames6.add("xml/base/arkhammain.xml");
		gdata.loadEncounters(filenames6);
		return gdata;
	}
	*/
	public static void loadInvestigatorCards(String filename) throws Exception
	{
		JAXBContext jc = JAXBContext.newInstance("arkham.jaxb.invcard");
		
		Unmarshaller um = jc.createUnmarshaller();
		Object obj = um.unmarshal(new File(filename));
		JAXBElement<InvCardSetType> elem = (JAXBElement<InvCardSetType>) obj;
		InvCardSetType xmlset = elem.getValue();
		for (int i = 0; i < xmlset.getCard().size(); i++)
		{
			InvestigatorCard invcard = InvestigatorCard.createCardFromXml(xmlset.getCard().get(i));
			if (invcard == null) System.exit(0);
			System.out.println(invcard);
		}
	}
	
	public static void loadEncounters(String filename) throws Exception
	{
		JAXBContext jc = JAXBContext.newInstance("arkham.jaxb.encounter");
		
		Unmarshaller um = jc.createUnmarshaller();
		Object obj = um.unmarshal(new File(filename));
		JAXBElement<CardSetType> elem = (JAXBElement<CardSetType>) obj;
		CardSetType xmlset = elem.getValue();
		/*
		for (int i = 0; i < xmlset.getMythos().size(); i++)
		{
			MythosCard mythoscard = MythosCard.createMythosFromXml(xmlset.getMythos().get(i));
			if (mythoscard == null) System.exit(0);
			System.out.println(mythoscard);
		}
		*/
		/*
		for (int i = 0; i < xmlset.getGate().size(); i++)
		{
			GateCard gateCard = GateCard.createGateCardFromXml(xmlset.getGate().get(i));
			if (gateCard == null) System.exit(0);
			System.out.println(gateCard);
		}
		*/
		/*
		for (int i = 0; i < xmlset.getRumor().size(); i++)
		{
			Rumor rumor = Rumor.createRumorFromXml(xmlset.getRumor().get(i));
			if (rumor == null) System.exit(0);
			System.out.println(rumor);
		}
		*/
		for (int i = 0; i < xmlset.getEnvironment().size(); i++)
		{
			Environment env = Environment.createEnvironmentFromXml(xmlset.getEnvironment().get(i));
			if (env == null) System.exit(0);
			System.out.println(env);
		}
	}
	
	public static void loadMonsters(String filename) throws Exception
	{
		JAXBContext jc = JAXBContext.newInstance("arkham.jaxb.monster");
		
		Unmarshaller um = jc.createUnmarshaller();
		Object obj = um.unmarshal(new File("xml/base/arkhammonsters.xml"));
		JAXBElement<MonsterSetType> elem = (JAXBElement<MonsterSetType>) obj;
		MonsterSetType xmlset = elem.getValue();
		for (int i = 0; i < xmlset.getMonster().size(); i++)
		{
			Monster monster = Monster.createMonsterFromXml(xmlset.getMonster().get(i));
			if (monster == null) System.exit(0);
			System.out.println(xmlset.getMonster().get(i).getQty() + " * " + monster);
		}
	}
	
	public static void loadAncientOnes(String filename) throws Exception
	{
		JAXBContext jc = JAXBContext.newInstance("arkham.jaxb.ancientone");
		
		Unmarshaller um = jc.createUnmarshaller();
		Object obj = um.unmarshal(new File(filename));
		JAXBElement<AOSetType> elem = (JAXBElement<AOSetType>) obj;
		AOSetType xmlao = elem.getValue();
		for (int i = 0; i < xmlao.getAncientOne().size(); i++)
		{
			AncientOne ao = AncientOne.createAncientOneFromXml(xmlao.getAncientOne().get(i));
			if (ao == null) System.exit(0);
			System.out.println(ao);	
		}
	}
}
